<?php

namespace Mana\Controller;

class RecordController extends CommController
{
    public function addto($type)
    {
        $db=D($type);
        if (!$db->create()) {
            $this->error($db->getError());
        } else {
            $db->add();
            $this->success("添加成功");
        }
    }
    
    public function edit($type, $str, $id)
    {
        $db=D($type);
        
        if (!$db->create(I('post.'), 2)) {
            $this->error($db->getError());
        } else {
            $this->removekey($db, $type, I('post.'));
            $db->where($str."=".$id)->save();
            $this->success("修改成功");
        }
    }
    
    public function stat($type, $str, $id)
    {
        $db=D($type);
        $data=$db->find($id);
        $db->$str=$data[$str]==1?0:1;
        if ($db->save()) {
            $this->success("处理成功");
        } else {
            $this->error("处理失败");
        }
    }
    
    public function del($type, $id)
    {
        $db=D($type);
        if ($db->delete($id)) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }
    
    private function removekey($db, $type, $data)
    {
        if ($type=="manager") {
            if ($data["mag_pass"]==null) {
                unset($db->mag_pass);
            }
        }
    }
}
